﻿using BrnMall.Manager.Domain.Blog;
using BrnMall.Manager.Manager.AddressBook;
using BrnMall.Manager.Manager.Blog;
using BrnMall.Manager.Manager.User;
using BrnMall.Services;
using QIDong.WeApp.Models;
using QIDong.WeApp.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WeiTap.Restful;

namespace QIDong.WeApp.Controllers
{
    /// <summary>
    /// 微街客-微博动态控制器
    /// </summary>
    public class BlogController : WebApi
    {

        #region 动态帖子
        /// <summary>
        /// 发布动态贴
        /// </summary>
        /// <param name="storeid">店铺ID</param>
        /// <param name="uid">会员ID</param>
        /// <param name="content">内容</param>
        /// <param name="imgs">图片，多个用逗号隔开</param>
        /// <returns></returns>
        public bool AddBlog(int storeid, int uid, string content = "", string imgs = "")
        {
            if (storeid < 0)
                throw new Exception("请填写店铺id");
            if (uid < 0)
                throw new Exception("用户id不能为空");
            if (string.IsNullOrEmpty(content) && string.IsNullOrEmpty(imgs))
                throw new Exception("内容和图片至少填一项");

            var model = new Blogs
            {
                StoreId = storeid,
                Uid = uid,
                Content = content,
                Addtime = DateTime.Now,
                Imgs = imgs
            };
            return BlogManager.InsertBlogs(model);
        }


        /// <summary>
        /// 增加点赞数
        /// </summary>
        /// <param name="blogId">动态贴ID</param>
        /// <param name="uid">评论人uid</param>
        /// <returns></returns>
        public bool AddUp(int blogId, int uid)
        {
            if (blogId < 1)
                throw new Exception("请选择帖子");
            if (uid < 1)
                throw new Exception("请输入用户");

            var old = BlogLikeManager.GetByBlogId(blogId, uid);
            if (old != null)
            {
                //取消点赞
                BlogLikeManager.DeleteByBlogId(blogId, uid);
                return BlogManager.AddUp(blogId, 2);
            }

            var userInfo = Users.GetPartUserById(uid);
            var model = new BrnMall.Manager.Domain.Blog.BlogLike
            {
                BlogId = blogId,
                Uid = userInfo.Uid,
                NickName = userInfo.NickName
            };

            if (BlogLikeManager.Insert(model))
            {
                BlogManager.AddUp(blogId, 1);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除动态贴
        /// </summary>
        /// <param name="storeid"></param>
        /// <param name="blogid"></param>
        /// <returns></returns>
        public bool DeleteBlog(int storeid, int blogid)
        {
            if (storeid < 0)
                throw new Exception("请填写店铺id。");
            if (blogid < 0)
                throw new Exception("帖子id不存在。");
            return BlogManager.DeleteBlogById(storeid, blogid);
        }

        /// <summary>
        /// 获取微博动态
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="uid"></param>
        /// <param name="PageNumber"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public object BlogList(int storeId, int uid, int PageNumber = 1, int PageSize = 15)
        {
            var bList = BlogManager.GetBlogList(storeId, uid, PageNumber, PageSize);
            //信息
            var list = new List<BlogsModel>();
            foreach (var item in bList.Items)
            {
                var info = new BlogsModel()
                {
                    Id = item.Id,
                    StoreId = item.StoreId,
                    Uid = item.Uid,
                    UpCount= item.UpCount,
                    Content = TenpayUtil.RemoveHtml(item.Content),
                    Imgs = item.Imgs,
                    Addtime = DateFormatToString(item.Addtime)
                };

                var user = StaffManager.GetByUid(item.Uid);
                if (user != null)
                {
                    info.NickName = user != null ? user.Name : string.Empty;
                    info.Avater = user != null ? user.Avater : string.Empty;
                }

                //点赞列表
                info.UpList = BlogLikeManager.GetList(item.Id);
                //评论列表
                info.CommentList = BlogCommentManager.GetList(item.Id, 50);

                list.Add(info);
            }

            var model = new
            {
                PageModel = new PageModel(PageSize, PageNumber, (int)bList.TotalItems),
                BlogList = list
            };

            return model;
        }

        /// <summary>
        /// 我的微博动态
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="uid"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public object MyBLogList(int storeId, int uid, int pageNumber = 1, int pageSize = 15)
        {
            var blogList = BlogManager.GetBlogList(storeId, uid, pageNumber, pageSize);

            //信息
            var list = new List<BlogsModel>();
            foreach (var item in blogList.Items)
            {
                var info = new BlogsModel()
                {
                    Id = item.Id,
                    Content = TenpayUtil.RemoveHtml(item.Content),
                    Imgs = item.Imgs,
                    UpCount = item.UpCount,
                    Addtime = item.Addtime.ToString("yyyy-MM-dd HH:mm:ss")
                };
                //评论列表
                info.CommentList = BlogCommentManager.GetList(item.Id, 50);

                list.Add(info);
            }
            var model = new
            {
                PageModel = new PageModel(pageNumber, pageSize, (int)blogList.TotalItems),
                BlogList = list
            };

            return model;
        }

        /// <summary>
        /// 帖子详情
        /// </summary>
        /// <param name="blogId"></param>
        /// <returns></returns>
        public object BlogInfo(int blogId)
        {

            var blogInfo = BlogManager.Get(blogId);
            if (blogInfo == null)
                return null;
            var userInfo = StaffManager.GetByUid(blogInfo.Uid);
            //点赞列表
            var UpList = BlogLikeManager.GetList(blogId);
            //评论列表
            var CommentList = BlogCommentManager.GetList(blogId, 50);

            var model = new
            {
                NickName = userInfo.Name,
                Avater = userInfo.Avater,
                BlogInfo = blogInfo,
                UpList = UpList,
                CommentList = CommentList
            };
            return model;
        }
        #endregion

        #region 帖子评论

        /// <summary>
        /// 对动态贴发起评论
        /// </summary>
        /// <param name="blogid"></param>
        /// <param name="uid"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool AddBlogComment(int blogid = 0, int uid = 0, string content = "")
        {
            if (blogid < 0)
                throw new Exception("帖子不存在。");
            if (uid < 0)
                throw new Exception("用户不存在。");
            if (content.Length > 500)
                throw new Exception("评论的内容超过限制。");
            BrnMall.Manager.Domain.User.User.UsersModel user = UsersManager.Get(uid);
            var model = new BlogComment { BlogId = blogid, Uid = uid, NickName = user.NickName, Addtime = DateTime.Now, Content = TenpayUtil.RemoveHtml(content) };
            return BlogCommentManager.Insert(model);
        }
        /// <summary>
        /// 取消评论
        /// </summary>
        /// <param name="blogid"></param>
        /// <returns></returns>
        public bool DeleteBlogComment(int blogid)
        {
            if (blogid < 0)
                throw new Exception("帖子id为空。");
            return BlogCommentManager.DeleteByBlogId(blogid);
        }

        /// <summary>
        /// 获取动态帖子评论列表
        /// </summary>
        /// <param name="blogid"></param>
        /// <returns></returns>
        public IList<BlogCommentModel> BlogCommentList(int blogid)
        {
            if (blogid < 0)
                throw new Exception("请输入帖子id");
            var bList = BlogCommentManager.GetByBlogId(blogid);
            var list = new List<BlogCommentModel>();
            foreach (var item in bList)
            {
                var model = new BlogCommentModel
                {
                    BlogId = item.BlogId,
                    Uid = item.Uid,
                    Addtime = item.Addtime,
                    NickName = item.NickName,
                    Content = item.Content
                };
                list.Add(model);
            }
            return list;
        }

        #endregion

        #region 帖子点赞
        /// <summary>
        /// 点赞列表
        /// </summary>
        /// <param name="blogid"></param>
        /// <returns></returns>
        public IList<Models.BlogLike> BlogLikeList(int blogid)
        {
            if (blogid < 0)
                throw new Exception("请输入帖子id");
            var bList = BlogLikeManager.GetList(blogid);
            var list = new List<Models.BlogLike>();
            foreach (var item in bList)
            {
                var model = new Models.BlogLike
                {
                    BlogId = item.BlogId,
                    NickName = item.NickName
                };
                list.Add(model);
            }
            return list;
        }

        #endregion

        #region 私有方法

        string DateFormatToString(DateTime dt)
        {
            TimeSpan span = (DateTime.Now - dt).Duration();
            if (span.TotalDays > 60)
            {
                return dt.ToString("yyyy-MM-dd");
            }
            else if (span.TotalDays > 30)
            {
                return "1个月前";
            }
            else if (span.TotalDays > 14)
            {
                return "2周前";
            }
            else if (span.TotalDays > 7)
            {
                return "1周前";
            }
            else if (span.TotalDays > 1)
            {
                return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
            }
            else if (span.TotalHours > 1)
            {
                return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
            }
            else if (span.TotalMinutes > 1)
            {
                return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
            }
            else if (span.TotalSeconds >= 1)
            {
                return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
            }
            else
            {
                return "1秒前";
            }
        }
        #endregion
    }
}